#![allow(dead_code)]
use std::time::Duration;

mod common;
mod hash;
mod list;

fn main() {
    common::init_logger();
    common::build_async_runtime(run_internal());
}

async fn run_internal() {
    let builder = redius::client::RedisClientBuilder::new_standalone("192.168.128.131", 6379)
        .password("111111")
        .reconnect_interval(Duration::from_secs(3))
        .receive_timeout(Duration::from_secs(30))
        .timeout(Duration::from_secs(55))
        .client_name("redius-example".to_string());
   let client = match builder.build_and_connect().await {
        Ok(client) => {
            client
            // list::lpush(&client).await;
            // list::lmove(&client).await;
            // list::blpop(&client).await;
            // hash::hset(&client).await;
            // hash::hgetall(&client).await;
        }
        Err(err) => {
            log::error!("{:?}", err);
            return ;
        }
    };
    let info = client.info(Some("server")).await.unwrap();
    log::info!("{info}");
    // let client2 = client.clone();
    // list::lpush(&client2).await;
    match client.subscribe_more(&["q1", "q2", "q3"]).await {
        Ok(mut stream) => {
            loop {
                match stream.recv().await {
                    Ok(Some(val)) => match String::from_utf8(val.content) {
                        Ok(str) => {
                            if str == "exit" {
                                log::debug!("subscribe exit");
                                break;
                            } else {
                                log::info!(
                                    "receive from channel = {}, contnet = {}",
                                    val.channel,
                                    str
                                )
                            }
                        }
                        Err(err) => {
                            log::error!("{err:?}");
                        }
                    },
                    Ok(None) => {
                        break;
                    }
                    Err(err) => {
                        log::error!("{err:?}");
                    }
                }
            }
            let _client2 = stream.exit().await;
        }
        Err(err) => {
            log::error!("{err:?}");
        }
    }
    log::info!("press ctrl+c key to exit");
    match tokio::signal::ctrl_c().await {
        Ok(_) => {
            log::info!("exit, bye bye ~");
        }
        Err(err) => {
            log::error!("{}", err);
        }
    }
}
